package com.controller;

import com.alibaba.fastjson.JSON;
import com.domain.Chunk;
import com.dto.JsonResponse;
import com.service.impl.LocalFileServiceImpl;
import com.util.FileUtilLocal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RequestMapping("/file")
@Slf4j
@RestController
public class LocalFileApi {

    @Autowired
    private LocalFileServiceImpl localFileServiceImpl;

    @PostMapping("/uploadPic")
    public JsonResponse<String> uploadPic(@RequestParam MultipartFile file, @RequestParam Integer type) throws IOException {
        String fileMD5 = localFileServiceImpl.uploadPic(file, type);
        return new JsonResponse<>(fileMD5);
    }

    @PostMapping("/uploadVideo")
    public JsonResponse<String> uploadVideo(MultipartFile file) throws Exception {
        String fileMD5 = localFileServiceImpl.uploadVideo(file);
        return new JsonResponse<>(fileMD5);
    }

    @GetMapping("/download")
    public void download(@RequestParam String fileMd5, HttpServletResponse response) throws IOException {
        localFileServiceImpl.download(fileMd5, response);
    }

    @GetMapping("/downloadVideo")
    public void downloadVideo(@RequestParam String fileMd5, HttpServletRequest request, HttpServletResponse response) throws IOException {
        localFileServiceImpl.downloadVideo(fileMd5, request, response);
    }

    @PostMapping("/uploadChunk")
    public JsonResponse<String> uploadChunk(@RequestParam("file") MultipartFile file, @RequestParam("form") String form) {
        Chunk chunk = JSON.parseObject(form, Chunk.class);
        String result = localFileServiceImpl.uploadChunk(file, chunk);
        return new JsonResponse<>(result, "文件上传成功", chunk.getChunkIndex().toString());
    }


    @PostMapping("/getFileMD5")
    public JsonResponse<String> getFileMD5(MultipartFile file) {
        log.info("getFileMD5 文件的名称：{} ", file.getOriginalFilename());
        String fileMD5 = FileUtilLocal.getMultiFileMD5(file);
        return new JsonResponse<>(fileMD5);
    }

    @GetMapping("/checkChunk")
    public JsonResponse<Map<String, Object>> checkChunk(@RequestParam("md5") String md5) {
        return localFileServiceImpl.checkChunk(md5);
    }

    @PostMapping("/mergeChunk")
    public JsonResponse<String> mergeChunk(@RequestBody Chunk chunk) {
        String mergeChunk = localFileServiceImpl.mergeChunk(chunk);
        return new JsonResponse<>(mergeChunk, "合并分片成功", null);
    }

    @PostMapping("/getFileSize")
    public Long getFileSize(MultipartFile file) {
        return file.getSize();
    }


    /**
     * 把分片的文件依次上传到 FastDFS 服务器中，并返回文件在FastDFS服务器的保存路径
     */
    @PostMapping("/file-slices")
    public JsonResponse<String> uploadFileBySlices(MultipartFile slice, String fileMd5,
                                                   Integer sliceNo, Integer totalSliceNo) throws IOException {
        log.info("文件的名称：{}", slice.getOriginalFilename());
        log.info("文件的大小：{}", slice.getSize());
        log.info("fileMd5：{}", fileMd5);
        String filePath = localFileServiceImpl.uploadFileBySlices(slice, fileMd5, sliceNo, totalSliceNo);
        return new JsonResponse<>(filePath);
    }
}
